/**
 * 全站通用
 * 参考wordpress
 */

(function ($, window, undefined) {
    "use strict";

    (function () {
        var timeout;

        function triggerEvent() {
            $(document).trigger('ycmf-window-resized');
        }

        function fireOnce() {
            window.clearTimeout(timeout);
            timeout = window.setTimeout(triggerEvent, 200);
        }

        $(window).on('resize.ycmf-fire-once', fireOnce);
    })();

    $(document).ready(function () {
        var $document = $(document),
            $window = $(window),
            $body = $(document.body),
            $wrap = $('#ycmf-wrap'),
            $adminMenu = $('#ycmf-admin-menu'),
            $adminMenuWrap = $('#ycmf-admin-menu-wrap'),
            $collapseMenu = $('#collapse-menu'),
            $foldMenu = $('#fold-menu'),
            stickyMenuActive = false,
            responsiveActive = false;

        if ($.cookie('admin-menu-fold') == '1') {
            $adminMenu.data('folded', 1);
        }

        var stickyMenu = {
            enable: function () {
                if (!stickyMenuActive) {
                    $document.on('ycmf-window-resized.sticky-menu', $.proxy(this.update, this));
                    $collapseMenu.on('click.sticky-menu', $.proxy(this.update, this));
                    this.update();
                    stickyMenuActive = true;
                }
            },

            disable: function () {
                if (stickyMenuActive) {
                    $window.off('resize.sticky-menu');
                    $collapseMenu.off('click.sticky-menu');
                    $body.removeClass('sticky-menu');
                    stickyMenuActive = false;
                }
            },

            update: function () {
                if ($window.height() > $adminMenuWrap.height() + 35) {
                    if (!$body.hasClass('sticky-menu')) {
                        $body.addClass('sticky-menu');
                    }
                } else {
                    if ($body.hasClass('sticky-menu')) {
                        $body.removeClass('sticky-menu');
                    }
                }
            }
        };

        var responsive = {
            width: 979,

            enable: function () {
                var scrollStart = 0;
                if (!responsiveActive) {
                    this.update();
                    $document.on('ycmf-window-resized.responsive', $.proxy(this.update, this));
                    $foldMenu.on('click.responsive', function () {
                        $wrap.toggleClass('responsive-open');
                    });

                    $adminMenuWrap.on('touchstart.responsive', 'li.not-current-menu > a',function () {
                        scrollStart = $window.scrollTop();
                    }).on('touchend.responsive click.responsive', 'li.not-current-menu > a', function (event) {
                        if (!$adminMenu.data('ycmf-responsive') ||
                            (event.type === 'touchend' && $window.scrollTop() !== scrollStart)) {
                            return;
                        }

                        $(this).parent('li').toggleClass('expand');
                        event.preventDefault();
                    });

                    responsiveActive = true;
                }
            },

            disable: function () {
                if (responsiveActive) {
                    $window.off('resize.responsive');
                    $foldMenu.off('click.responsive');
                    $body.removeClass('auto-fold');
                    $wrap.removeClass('responsive-open');
                    responsiveActive = false;
                }
            },

            update: function () {
                if (window.innerWidth) {
                    this.width = Math.max(window.innerWidth, document.documentElement.clientWidth);
                }
                if (this.width <= 979) {
                    if (!$adminMenu.data('ycmf-responsive')) {
                        $adminMenu.data('ycmf-responsive', 1);

                        if (!$body.hasClass('auto-fold')) {
                            $body.addClass('auto-fold');
                        }

                        $collapseMenu.hide();
                        stickyMenu.disable();
                    }
                } else {
                    if ($adminMenu.data('ycmf-responsive')) {
                        $adminMenu.data('ycmf-responsive', 0);

                        $body.removeClass('auto-fold');
                        $collapseMenu.show();
                        stickyMenu.enable();
                        $wrap.removeClass('responsive-open');
                    }
                }
            }
        };

        $adminMenu.find('li.has-sub').hoverIntent({
            over: function () {
                if ($(this).hasClass('current-menu')) {
                    return;
                }

                if ($adminMenu.data('ycmf-responsive')) {
                    return;
                }

                var m = $(this).find('.sub-nav'),
                    top = m.offset().top,
                    h = m.height(),
                    winH = $window.height(),
                    winTop = $window.scrollTop(),
                    b = winTop + winH - top - 36,
                    o = h - b - 29 + 15;

                if (o > winH) {
                    o = o - winH;
                }

                if (o > 1) {
                    m.css('margin-top', '-' + o + 'px');
                    if ($adminMenu.data('folded')) {
                        $(this).find('.menu-name').css('margin-top', '-' + o + 'px');
                    }
                } else {
                    m.css('margin-top', '');
                    if ($adminMenu.data('folded')) {
                        m.find('.menu-name').css('margin-top', '');
                    }
                }

                $adminMenu.find('li.menu-top').removeClass('open-sub');
                $(this).addClass('open-sub');
            },

            out: function () {
                if ($(this).hasClass('current-menu')) {
                    return;
                }

                if ($adminMenu.data('ycmf-responsive')) {
                    return;
                }

                $(this).find('.sub-nav').css('margin-top', '');
                if ($adminMenu.data('folded')) {
                    $(this).find('.menu-name').css('margin-top', '');
                }

                $(this).removeClass('open-sub');
            },
            timeout: 200,
            sensitivity: 7,
            interval: 90
        });

        $adminMenu.find('.has-sub a').on('focus.admin-menu',function (e) {
            if ($adminMenu.data('ycmf-responsive')) {
                return;
            }

            $(e.target).closest('li.top-menu').addClass('open-sub');
        }).on('blur.admin-menu', function (e) {
            if ($adminMenu.data('ycmf-responsive')) {
                return;
            }

            $(e.target).closest('li.top-menu').removeClass('open-sub');
        });

        $collapseMenu.on('click.collapse-menu', function () {
            $body.toggleClass('folded');

            if ($body.hasClass('folded')) {
                $adminMenu.data('folded', 1);
                $.cookie('admin-menu-fold', 1);
            } else {
                $adminMenu.data('folded', 0);
                $.cookie('admin-menu-fold', 0);
            }
        });

        if (window.innerWidth) {
            stickyMenu.enable();
            responsive.enable();
        }

        (function () {
            var $formSide = $('#form-side');
            var $formMain = $('#form-main');
            if (!$formSide.length) {
                return;
            }

            var formSideTop;

            function formSideFixed() {
                if (($formMain.height() < $formSide.height()) || (formSideTop > $window.scrollTop() + 45)) {
                    if ($formSide.hasClass('fixed')) {
                        $formSide.css('width', 'auto').removeClass('fixed');
                    }
                } else {
                    if (!$formSide.hasClass('fixed')) {
                        $formSide.css('width', $formSide.width()).addClass('fixed');
                    }
                }
            }

            formSideTop = $formSide.offset()['top'];
            $window.on('resize',function () {
                formSideTop = $formSide.offset()['top'];
                formSideFixed();
            }).on('scroll', formSideFixed);
        })();

        (function () {
            var $formActions = $('.form-actions');

            if (!$formActions.length) {
                return;
            }

            var timeInterval,
                ready = true,
                formActionsTop;

            function formActionsFixed() {
                if (formActionsTop < $window.scrollTop() + $window.height()) {
                    if ($formActions.hasClass('fixed')) {
                        $formActions.css('width', 'auto').removeClass('fixed');
                    }
                } else {
                    if (!$formActions.hasClass('fixed')) {
                        $formActions.css('width', $formActions.width()).addClass('fixed');
                    }
                }
            }

            var timeout = function () {
                if (window.CKEDITOR) {
                    $.each(window.CKEDITOR.instances, function (name, editor) {
                        if (editor.status == 'ready') {
                            ready = true;
                        } else {
                            ready = false;
                        }
                    });
                }
                if (ready) {
                    if (window.CKEDITOR) {
                        $.each(window.CKEDITOR.instances, function (name, editor) {
                            editor.on('resize', function (evt) {
                                $.cookie('editor_'+ name,evt.editor.ui.space('contents').getStyle( 'height' ));
                                formActionsTop = $formActions.offset()['top'];
                                formActionsFixed();
                            });
                        })
                    }

                    formActionsTop = $formActions.offset()['top'];
                    $window.on('resize',function () {
                        formActionsTop = $formActions.offset()['top'];
                        formActionsFixed();
                    }).on('scroll', formActionsFixed);
                    clearTimeout(timeInterval);
                } else {
                    timeInterval = setTimeout(timeout, 10);
                }
            };

            timeInterval = setTimeout(timeout, 10);
        })();
    });
})(jQuery, window);